package dao;

import java.util.ArrayList;

import modelo.Cargo;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DaoCargo {
	private SessionFactory fabrica;

	public DaoCargo()throws Exception
	{	fabrica=new Configuration().addClass(modelo.Cargo.class).buildSessionFactory();
		
	}
	public void cadastrar(Cargo f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.save(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	
	public ArrayList<Cargo> consultarTodos(){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cargo");
		ArrayList<Cargo> cargos = (ArrayList<Cargo>)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return cargos;
	}
	
	
	public Cargo consultar(Cargo f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		f=(Cargo)sessao.get(modelo.Cargo.class,f.getId_cargo());
		
		transacao.commit();
		sessao.flush();
		sessao.close();
		return f;
	}
	public void excluir(Cargo f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.delete(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public void alterar(Cargo f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.update(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	
	public ArrayList<Cargo> consultarTodosNome(String nome){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Cargo where nome_cargo like '%" + nome + "%' order by nome_cargo");
		ArrayList<Cargo> cargos=(ArrayList<Cargo>)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return cargos;
	}
	
	
}
